package cn.wolfcode.trip.admin;

import cn.wolfcode.trip.core.domain.Travel;
import cn.wolfcode.trip.core.domain.TravelContent;
import cn.wolfcode.trip.core.query.TravelQueryObject;
import cn.wolfcode.trip.core.service.ITravelService;
import cn.wolfcode.trip.core.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    /**
     * 查询待审核页面
     *
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") TravelQueryObject qo, Model model) {
        // 默认查询待审核的游记，也可以使用下拉框查询已拒绝的游记
        if (qo.getState()==null) {
            qo.setState(Travel.STATE_WAIT);//待审核
        }
        qo.setIsPublic(true);//公开的
        //按照最后更新时间正序排序
        qo.setOrderBy("lastUpdateTime ASC");

        model.addAttribute("pageInfo",travelService.query(qo));
        return "travel/list";
    }

    /**
     * 查询游记内容
     * @param id
     * @return
     */
    @RequestMapping("getContentById")
    @ResponseBody
    public TravelContent getContentById(Long id){

        return travelService.getContentById(id);
    }

    /**
     * 查看游记发布页面
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("releaseList")
    public String releaseList(@ModelAttribute("qo") TravelQueryObject qo, Model model){
        // 默认查询已发布状态的游记
        qo.setState(Travel.STATE_RELEASE);
        qo.setIsPublic(true);//公开的
        // 按照发布时间倒序排序
        qo.setOrderBy("releaseTime DESC");
        model.addAttribute("pageInfo",travelService.query(qo));
        return "travel/releaseList";
    }

    @RequestMapping("updateState")
    @ResponseBody
    public JsonResult updateState(Travel travel){

        travelService.updateState(travel);
        return new JsonResult();
    }
}
